<?php

/**
 * 返回可读性更好的文件尺寸
 */
function human_filesize($bytes, $decimals = 2)
{
    $size = ['B', 'kB', 'MB', 'GB', 'TB', 'PB'];
    $factor = floor((strlen($bytes) - 1) / 3);

    return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) .@$size[$factor];
}

/**
 * 判断文件的MIME类型是否为图片
 */
function is_image($mimeType)
{
    return starts_with($mimeType, 'image/');
}
/**
 *返回汉字首字母
 */
function getFirstCharter($str)
{
    if (empty($str)) {
        return '';
    }
    $fchar = ord($str{0});
    if ($fchar >= ord('A') && $fchar <= ord('z')) return strtoupper($str{0});
    $s1 = iconv('UTF-8', 'gb2312', $str);
    $s2 = iconv('gb2312', 'UTF-8', $s1);
    $s = $s2 == $str ? $s1 : $str;
    $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
    if ($asc >= -20319 && $asc <= -20284) return 'a';
    if ($asc >= -20283 && $asc <= -19776) return 'b';
    if ($asc >= -19775 && $asc <= -19219) return 'c';
    if ($asc >= -19218 && $asc <= -18711) return 'd';
    if ($asc >= -18710 && $asc <= -18527) return 'e';
    if ($asc >= -18526 && $asc <= -18240) return 'f';
    if ($asc >= -18239 && $asc <= -17923) return 'g';
    if ($asc >= -17922 && $asc <= -17418) return 'h';
    if ($asc >= -17417 && $asc <= -16475) return 'j';
    if ($asc >= -16474 && $asc <= -16213) return 'k';
    if ($asc >= -16212 && $asc <= -15641) return 'l';
    if ($asc >= -15640 && $asc <= -15166) return 'm';
    if ($asc >= -15165 && $asc <= -14923) return 'n';
    if ($asc >= -14922 && $asc <= -14915) return 'o';
    if ($asc >= -14914 && $asc <= -14631) return 'p';
    if ($asc >= -14630 && $asc <= -14150) return 'q';
    if ($asc >= -14149 && $asc <= -14091) return 'r';
    if ($asc >= -14090 && $asc <= -13319) return 's';
    if ($asc >= -13318 && $asc <= -12839) return 't';
    if ($asc >= -12838 && $asc <= -12557) return 'w';
    if ($asc >= -12556 && $asc <= -11848) return 'x';
    if ($asc >= -11847 && $asc <= -11056) return 'y';
    if ($asc >= -11055 && $asc <= -10247) return 'z';
    return null;
}
/*数组null代替空字符*/
function unsetNull($arr){
    if($arr !== null){
        if(is_array($arr)){
            if(!empty($arr)){
                foreach($arr as $key => $value){
                    if($value === null){
                        $arr[$key] = '';
                    } else {
                        $arr[$key] = unsetNull($value);      //递归再去执行
                    }
                }
            }
        } else {
            if($arr === null) { //注意三个等号
                $arr = ''; 
            }         
        }
    } else { 
        $arr = ''; 
    }
    return $arr;
}

/*过滤字段，添加或修改过滤允许的字段*/
function filterAllowFields($allowFields, $data)
{
    $returnData = array();
    foreach($allowFields as $k=>$v) {
        if(is_array($v)) { 
            if(isset($data[$k])) {
                $arrFilter = filterAllowFields($v, $data[$k]);
                $returnData[$k] = $arrFilter;
            }   
        } else {
            if(isset($data[$v])) {
                $returnData[$v] = $data[$v];
            }
        }
    }
    return $returnData;
}

//BD-09(百度)坐标转换成GCJ-02(火星，高德)坐标  
//@param bd_lon 百度经度  
//@param bd_lat 百度纬度  
function bd_decrypt($bd_lon, $bd_lat) {  
    $x_pi = 3.14159265358979324 * 3000.0 / 180.0;  
    $x = $bd_lon - 0.0065;  
    $y = $bd_lat - 0.006;  
    $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $x_pi);  
    $theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi);  
    // $data['gg_lon'] = $z * cos($theta);  
    // $data['gg_lat'] = $z * sin($theta);  
    $gg_lon = $z * cos($theta);  
    $gg_lat = $z * sin($theta);  
    // 保留小数点后六位  
    $data['gg_lon'] = round($gg_lon, 6);  
    $data['gg_lat'] = round($gg_lat, 6);  
    return $data;  
}  
  
//GCJ-02(火星，高德)坐标转换成BD-09(百度)坐标  
//@param bd_lon 百度经度  
//@param bd_lat 百度纬度  
function bd_encrypt($gg_lon, $gg_lat) {  
    $x_pi = 3.14159265358979324 * 3000.0 / 180.0;  
    $x = $gg_lon;  
    $y = $gg_lat;  
    $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $x_pi);  
    $theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi);  
    $bd_lon = $z * cos($theta) + 0.0065;  
    $bd_lat = $z * sin($theta) + 0.006;  
    // 保留小数点后六位  
    $data['bd_lon'] = round($bd_lon, 6);  
    $data['bd_lat'] = round($bd_lat, 6);  
    return $data;  
}  
function iconv_arr($data){
     if(is_array($data)){
        foreach($data as $k=>$v){
               $data[$k] = iconv_arr($v);
        }
     }else{
        $data = mb_convert_encoding($data, "html-entities","utf-8" );
     }
     return $data;
}

function write($a,$end='0'){
	    if($a===''){
	        $b='空';
	    }elseif($a===true){
	        $b='true';
	    }elseif($a===false){
	        $b='false';
	    }elseif($a===null){
	        $b='null';
	    }elseif(is_object($a)){
	        $b=json_encode($a);
			// $b="obj";
	    }else{
	        $b=spread($a);
	    }
	    $filepath=$_SERVER['DOCUMENT_ROOT'].'/aaa.txt';
	    // if(!is_writable($filepath)){
	    //     return '没有写入权限';
	    // }
	    if($end){
	        file_put_contents($filepath,$b,FILE_APPEND);
	        return '写入成功';
	    }else{
	        file_put_contents($filepath,$b);
	        return '写入成功';
	    }
	}
	function spread($a,$space=''){
	    $space.='    ';
	    $b='';
	    if(is_array($a)){
	        $b.="array{\r\n";
	        foreach($a as $k=>$v){
	            if(is_numeric($k)){
	                $b.=$space.'['.$k.']'.'=>';
	            }else{
	                $b.=$space.'[\''.$k.'\']'.'=>';
	            }
	            $b.=spread($v,$space);
	        }
	        $b.=$space.'}'."\r\n";
	    }elseif(is_object($a)){
			$b.='obj';
	    }else{
	        $b.=$a."\r\n";
	    }
	    return $b;
	}
	